package com.zzyl.common.Test;

import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class QianfanDirectClientzzzz {

    // API地址
    private static final String API_URL = "https://qianfan.baidubce.com/v2/chat/completions";
    
    // 授权令牌，替换为你的实际令牌
    private static final String AUTH_TOKEN = "bce-v3/ALTAK-FoQYI7X2oyGY98mmTmvkE/37b0a18e86dc5716f6e73608ed5a841c33adeb28";

    public static void main(String[] args) {
        // 直接在main方法中执行所有操作，可直接右键运行
        RestTemplate restTemplate = new RestTemplate();

        // 创建请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Authorization", AUTH_TOKEN);

        // 构建请求体
        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("model", "ernie-3.5-8k");
        
        // 构建messages数组
        List<Map<String, String>> messages = new ArrayList<>();
        
        // 添加system消息
        Map<String, String> systemMessage = new HashMap<>();
        systemMessage.put("role", "system");
        systemMessage.put("content", "平台助手");
        messages.add(systemMessage);
        
        // 添加user消息
        Map<String, String> userMessage = new HashMap<>();
        userMessage.put("role", "user");
        userMessage.put("content", "你好");
        messages.add(userMessage);
        
        requestBody.put("messages", messages);

        // 创建HTTP请求实体
        HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(requestBody, headers);
        
        try {
            // 发送POST请求并获取响应
            ResponseEntity<String> response = restTemplate.postForEntity(API_URL, requestEntity, String.class);
            
            // 输出响应结果
            System.out.println("请求状态码: " + response.getStatusCodeValue());
            System.out.println("响应内容: " + response.getBody());
        } catch (Exception e) {
            System.err.println("请求出错: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
